package com.eso.haBank.controller

import com.eso.frame.handler.IReloadDataHandler
import com.eso.frame.task.TaskException

/**
 * 重新加载数据或手动执行定时任务
 */
class ReloadController {

    IReloadDataHandler reloadDataHandler;

    def index() {
        def taskIdentity = params?.taskIdentity;

        Integer resultCode = 0
        String resultMsg = "";

        try {
            if (reloadDataHandler.isJobAutoRunning(taskIdentity)) {
                log.info("reload the named of $taskIdentity task will be stop, because it's running.")
                return render(contentType: "text/json") {
                    Status: 9
                    Message: "${message(code: 'reload.task.result.isRunning')}"
                }
            }

            reloadDataHandler.executeReloadDataTask(taskIdentity);

            resultCode = 1;
            resultMsg = "${message(code: 'reload.task.result.success')}";

        } catch (TaskException e) {
            resultCode = 0;
            resultMsg = "${message(code: 'reload.task.result.failure')}";

            log.error e;
        }

        log.info "reload the named of $taskIdentity task,result{Status:$resultCode, Message:'$resultMsg'}.";

        render(contentType: "text/json") {
            Status: resultCode
            Message: resultMsg
        }
    }
}
